<!--
 * @Description: 社区页面
 * @Author: Gaq
 * @Date: 2022-10-16 17:27:41
 * @LastEditTime: 2022-11-02 10:27:58
 * @LastEditors: Gaq
-->
<script setup>
import request from "@/utils/request";
import router from "@/router";

let post = ref({
  detail: "",
});
let postItem = ref([]);

const isEmpty = ref(false);
const isLoading = ref(false);

onBeforeMount(() => {
  isLoading.value = true;
  getPostList();
});

const getPostList = () => {
  console.log(post.value.detail);
  request.post(`/post/getPart`, post.value).then((res) => {
    if (res.code == -1) {
      console.log(res.msg);
      isEmpty.value = true;
    } else {
      postItem.value = res.data.postList;
      isEmpty.value = false;
      isLoading.value = false;
    }
  });
};

const goPostDetail = (postId) => {
  const routeUrl = router.resolve({
    name: "postDetail",
    query: { postId },
  });
  window.open(routeUrl.href, "_blank");
};

const goToAddPost = () => {
  const postId = "";
  const routeUrl = router.resolve({
    name: "postEditor",
    query: { postId },
  });
  window.open(routeUrl.href, "_blank");
};
</script>

<template>
  <div class="box">
    <div class="main">
      <div class="inputSearch">
        <input
          type="text"
          placeholder="请输入你想要搜索的内容"
          v-model="post.detail"
          @keyup.enter="getPostList()"
        />
        <div class="icon" @click="getPostList()">
          <i-ep-Search />
        </div>
      </div>

      <div v-show="!isEmpty">
        <span v-show="isLoading" style="font-size: large">加载中......</span>
        <div
          v-masonry
          transition-duration="0.3s"
          item-selector=".item"
          class="list"
          v-show="postItem.length"
        >
          <div
            v-masonry-tile
            class="item"
            v-for="(post, index) in postItem"
            @click="goPostDetail(post.postId)"
          >
            <el-card :body-style="{ padding: '0px' }">
              <img :src="post.image" class="image" />
              <div style="padding: 10px">
                <div class="bottom">
                  <span>{{ post.title }}</span>
                </div>
              </div>
            </el-card>
          </div>
        </div>
      </div>

      <div class="empty" v-show="isEmpty">
        <el-empty description="暂时没有您想要的帖子~~" />
      </div>
    </div>

    <div class="add" @click="goToAddPost">
      <el-icon :size="60" color="#ff0000">
        <i-ep-Plus />
      </el-icon>
    </div>
  </div>
</template>

<style scoped>
.box {
  width: 100vw;
  height: 100vh;
}

.main {
  width: 90%;
  height: 100vh;
  margin: 0 auto;
  overflow: auto !important;
}

.main::-webkit-scrollbar {
  display: none;
}

.add {
  width: 60px;
  height: 60px;
  border-radius: 30px;
  text-align: center;
  box-shadow: rgba(60, 64, 67, 0.3) 0px 1px 2px 0px,
    rgba(60, 64, 67, 0.15) 0px 2px 6px 2px;
  position: absolute;
  z-index: 999;
  top: 0;
  bottom: 0;
  right: 0;
  margin: auto;
}

.inputSearch {
  border-radius: 3px;
  height: 35px;
  position: relative;
  border: 1px solid rgb(177, 176, 176);
  display: flex;
  margin: 20px auto;
  width: 60%;
}

.inputSearch:focus-within {
  border: 1px solid #6faad1;
  color: #6faad1;
}

input {
  background-color: transparent;
  caret-color: #6faad1;
  border: none;
  outline: none;
}

.icon {
  top: 20%;
  position: absolute;
  right: 0;
  cursor: pointer;
}

.list {
  display: flex;
  flex-wrap: wrap;
}

.item {
  margin: 15px;
  display: flex;
  flex-direction: column;
  width: calc(100% / 4 - 40px);
  -webkit-transform: perspective(1px) translateZ(0);
  transform: perspective(1px) translateZ(0);
  box-shadow: 0 0 1px rgba(0, 0, 0, 0);
  -webkit-transition-duration: 0.3s;
  transition-duration: 0.3s;
  -webkit-transition-property: box-shadow, transform;
  transition-property: box-shadow, transform;
}
.item:hover,
.item:focus,
.item:active {
  cursor: pointer;
  box-shadow: 0 10px 10px -10px rgba(0, 0, 0, 0.5);
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}

.image {
  width: 100%;
}

.bottom {
  margin-top: 10px;
  line-height: 15px;
  display: -webkit-box;
  justify-content: space-between;
  align-items: center;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  font-size: 12px;
  color: rgb(74, 72, 72);
}
</style>